package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.entity.dto.OrdersDto;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private OrderService ordersService;
@Autowired
    private OrderDetailService orderDetailService;
    /**
     * 订单明细
     * @return
     */
    @PostMapping("/submit")
    public R orders(@RequestBody Orders orders){
        log.info("订单明细为：{}",orders);
        ordersService.sumbit(orders);
        return R.success("下单成功");
    }

    /**
     * 订单明细
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R page(int page,int pageSize,String number,String beginTime,String endTime){
        //获取员工id
        log.info("page:{},pageSize:{},number:{},startTime:{},endTime:{}",page,pageSize,number,beginTime,endTime);
    Long currentId = BaseContext.getCurrentId();
    Page<Orders> pageInFo = new Page<>(page, pageSize);
    LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
    lqw.like(number != null ,Orders::getNumber,number);
    lqw.ge( beginTime != null, Orders::getOrderTime,beginTime);
    lqw.lt(endTime != null,Orders::getOrderTime,endTime);
    lqw.orderByDesc(Orders::getOrderTime);
    //lqw.eq(Orders::getUserId,currentId);
   ordersService.page(pageInFo,lqw);
    return R.success(pageInFo);

    }


    /**
     * 配送
     */
    @PutMapping
    public R<String> order(@RequestBody Orders orders){
        //log.info("orders:{}", orders);
        Orders order = ordersService.getById(orders.getId());
        if (order.getStatus() == 2){
            orders.setStatus(3);
            ordersService.updateById(orders);
            return R.success("订单派送成功");
        }else {
            orders.setStatus(4);
            ordersService.updateById(orders);
            return R.success("订单已完成");
        }
    }

    /**
     * 查询历史订单
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R page(int page ,int pageSize){
        log.info("查询page:{},pageSize:{}",page,pageSize);
        //获取用户id
        Long currentId = BaseContext.getCurrentId();
        Page<Orders> pageInFo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        //用户id比对
        lqw.eq(Orders::getUserId,currentId);
        //分页查询
        ordersService.page(pageInFo, lqw);
        //创建一个新的分页构造器   实体类为dto
        Page<OrdersDto> objectPage = new Page<>();
        //将pageInfo中的数据拷贝到 新的分页构造器中
        BeanUtils.copyProperties(pageInFo,objectPage,"records");
        //过去查询 dto的记录
        List<Orders> ordersList = pageInFo.getRecords();
        ArrayList<OrdersDto> orders1 = new ArrayList<>();
        int i = 0;
        //遍历获取查询的list集合
        for (Orders orders : ordersList) {
            OrdersDto ordersDto1 = new OrdersDto();
            //将遍历的list集合中的数据拷贝到新创建的对象中
            ordersDto1.setAddress(orders.getAddress());
            ordersDto1.setPhone(orders.getPhone());
            ordersDto1.setUserName(orders.getUserName());
            ordersDto1.setConsignee(orders.getConsignee());
            ordersDto1.setStatus(orders.getStatus());
            ordersDto1.setOrderTime(orders.getOrderTime());


            //获取dto的id
            //根据条件获取orderDetail集合中的数据
            LambdaQueryWrapper<OrderDetail> listlqw = new LambdaQueryWrapper<>();
            listlqw.eq(OrderDetail::getOrderId,orders.getId());
            List<OrderDetail> list = orderDetailService.list(listlqw);


            //将获取的集合添加到新创建的对象中
            ordersDto1.setOrderDetails(list);

            for (OrderDetail orderDetail : list) {
                //将orderDetail中的数量添加到orderdto中
                Integer number = orderDetail.getNumber();
                i =+ number;
            }
            ordersDto1.setAmount(orders.getAmount());
            ordersDto1.setSumNum(i);
            orders1.add(ordersDto1);
        }
        //添加查询记录
        objectPage.setRecords(orders1);
        return R.success(objectPage);
    }

    /**
     * z再来一单
     * @param orders
     * @return
     */

    @PostMapping("/again")
    public R again(@RequestBody Orders orders){
        log.info("再来一单",orders);
        //获取用户id
        Long currentId = BaseContext.getCurrentId();
        //清空购物车
        LambdaQueryWrapper<ShoppingCart> relqw = new LambdaQueryWrapper<>();
        relqw.eq(ShoppingCart::getUserId,currentId);
        shoppingCartService.remove(relqw);
        LambdaQueryWrapper<OrderDetail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(OrderDetail::getOrderId ,orders.getId());
        List<OrderDetail> list = orderDetailService.list(lqw);
        List<ShoppingCart> shoppingCartList = new ArrayList<>();
        for (OrderDetail orderDetail : list) {
            ShoppingCart shoppingCart = new ShoppingCart();
            BeanUtils.copyProperties(orderDetail,shoppingCart,"id","orderId");
            shoppingCart.setUserId(currentId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartList.add(shoppingCart);
        }
        shoppingCartService.saveBatch(shoppingCartList);
        return R.success("成功");
    }
}
